import $service from '../../../../../service.js';
var hospital_id="1";
var that;
var inquiry_patient_id;
var doctor_id;
var obj={};
var page=0;
var getMap;//获取用药map
var druglistStorage_z =[];//已选中药处方 列表
var druglistStorage_x =[];//已选西药处方 列表
var tapMap_type;
Page({
    data:{
       tapactive : 1,
       drugList :[],
       drugList2 :[],
       drugList3 :[],
       drugList4 :[],
       drug_name:'',
       drug_type:1,
       loadingText: '',
       loadingType: true,
       class_name:'',
       drugClassList:[],
       isshow:false,
       class_id:'',
       isdetalis:false,//查看详情
       diagnosisMap:[],//模板详情
       prescriptionMap:{}//模板详情列表
    },
    
    onLoad(e){
        obj = $service.strToJson(e.obj)
        this.setData({
            drug_type : obj.drug_type
        })
    },
    onShow(){
        that=this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if(!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)){
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        this.geturldo()
        
    },
    geturldo(){
        if(that.tapactive == 1 || that.tapactive == 2 ){
            this.doctorSide_personal_prescription_getDrugImg()
        }else if(that.tapactive == 3){
            this.doctorSide_personal_prescription_getDoctorDrugList()
        }else if(that.tapactive == 4 || that.tapactive == 5 ){
            this.doctorSide_personal_prescription_getUtilPrescription()
        }else if(that.tapactive == 6){
            this.doctorSide_personal_prescription_getHistoryPrescriptionList()
        }
    },
    onPullDownRefresh() {
        page = 0;
        this.setData({
            loadingText: '加载更多'
        })
        this.geturldo();
        wx.stopPullDownRefresh()
    },
    onReachBottom: function() {
        if (that.loadingType == true) {
            page++
            this.geturldo(); 
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    loading(){
        if (that.loadingType == true) {
            page++
            this.geturldo(); 
        } else {
            this.setData({
                loadingText: '没有更多了'
            })
        }
    },
    //西药、中药列表、分类
    doctorSide_personal_prescription_getDrugImg:function(){
        var jsonData ={
            "hospital_id" : hospital_id,
            "drug_type" : obj.drug_type,
            "class_id" : that.class_id,
            "page" : page,
            "drug_name" : this.data.drug_name
        }
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getDrugImg.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getDrugImg_back);
    },
    //回调
    doctorSide_personal_prescription_getDrugImg_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var drugList = data.drugList;//药品列表
        var drugClassList = data.drugClassList;//药品分类列表
        for(var i = 0; i < drugList.length; i++){
            if(obj.drug_type == 2){
                if(!$service.isUndefinedAndEmpty(drugList[i].drug_tagging)){
                    drugList[i].allindex = drugList[i].drug_tagging.split(';');
                 }else{
                     drugList[i].allindex = []
                 }
               
            }
            drugList[i].drug_usname = $service.nothingFun(drugList[i].drug_usname,'--')
        }
        if (page == 0) {
        	that.loadingText = '';
        	that.drugList = drugList;
        } else {
        	if (drugList.length <= 0) {
        		that.loadingType = false;
        		that.loadingText = '没有更多了';
        	} else {
        		for (var i = 0; i < drugList.length; i++) {
        			that.drugList.push(drugList[i]);
        			that.loadingText = '加载更多';
        		}
        	}
        }
         this.setData({
             drugList : that.drugList,
             loadingText: that.loadingText,
             loadingType: that.loadingType,
             drugClassList: drugClassList
         })
    },
    //个人用药列表
    doctorSide_personal_prescription_getDoctorDrugList:function(){
        var  icd_id="0"
        var jsonData ={
            "hospital_id" : hospital_id,
            "icd_id" : icd_id,
            "doctor_id" : doctor_id,
            "page" : page,
            "drug_name" : '',
            "drug_type" : obj.drug_type
        }
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getDoctorDrugList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getDoctorDrugList_back);
    },
    //回调
    doctorSide_personal_prescription_getDoctorDrugList_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var drugList = data.drugList;//药品列表
        for(var i = 0; i < drugList.length; i++){
            drugList[i].drug_usname = $service.nothingFun(drugList[i].drug_usname,'--')
        }
        if (page == 0) {
        	that.loadingText = '';
        	that.drugList2 = drugList;
        } else {
        	if (drugList.length <= 0) {
        		that.loadingType = false;
        		that.loadingText = '没有更多了';
        	} else {
        		for (var i = 0; i < drugList.length; i++) {
        			that.drugList2.push(drugList[i]);
        			that.loadingText = '加载更多';
        		}
        	}
        }
         this.setData({
             drugList2 : that.drugList2,
             loadingText: that.loadingText,
             loadingType: that.loadingType
         })
    },
    //个人、公共模板
    doctorSide_personal_prescription_getUtilPrescription:function(){
        var  icd_id=""
        if(this.data.tapactive == 4){
            var doctorid = 0
        }else{
            var doctorid = doctor_id
        }
        var jsonData ={
            "hospital_id" : hospital_id,
            "icd_id" : icd_id,
            "doctor_id" : doctorid,
            "queryName" : '',
            "type" : obj.drug_type,
            "page":page
        }
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getUtilPrescription.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getUtilPrescription_back);
    },
    //回调
    doctorSide_personal_prescription_getUtilPrescription_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var drugList = data.prescriptionList;//列表
        if (page == 0) {
        	that.loadingText = '';
        	that.drugList3 = drugList;
        } else {
        	if (drugList.length <= 0) {
        		that.loadingType = false;
        		that.loadingText = '没有更多了';
        	} else {
        		for (var i = 0; i < drugList.length; i++) {
        			that.drugList3.push(drugList[i]);
        			that.loadingText = '加载更多';
        		}
        	}
        }
         this.setData({
             drugList3 : that.drugList3,
             loadingText: that.loadingText,
             loadingType: that.loadingType
         })
    },
    //历史处方
    doctorSide_personal_prescription_getHistoryPrescriptionList:function(){
        var  icd_id=""
        var jsonData={
            "inquiry_patient_id" : obj.inquiry_patient_id,
            "prescription_type" : obj.drug_type
        } 
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getHistoryPrescriptionList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getHistoryPrescriptionList_back);
    },
    //回调
    doctorSide_personal_prescription_getHistoryPrescriptionList_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var drugList = data.prescriptionList;//列表
        if (page == 0) {
        	that.loadingText = '';
        	that.drugList4 = drugList;
        } else {
        	if (drugList.length <= 0) {
        		that.loadingType = false;
        		that.loadingText = '没有更多了';
        	} else {
        		for (var i = 0; i < drugList.length; i++) {
        			that.drugList4.push(drugList[i]);
        			that.loadingText = '加载更多';
        		}
        	}
        }
        for(var i = 0; i < that.drugList4.length; i++){
            that.drugList4[i].lrsj2 = that.drugList4[i].lrsj.substring(0, 10)
        }
         this.setData({
             drugList4 : that.drugList4,
             loadingText: that.loadingText,
             loadingType: that.loadingType
         })
    },
    //tab切换
    taptab(e){
        console.log(e)
        var index = e.currentTarget.dataset.index;
        if(this.data.tapactive == 1){
            that.class_id =''
            that.class_name = '全部'
        }else{
            
            if(that.drugClassList.length == 0){
                that.class_id = ''
                that.class_name = ''
            }else{
                if(that.drug_type == 2){
                    that.class_id = that.drugClassList[0].class_id
                    that.class_name = that.drugClassList[0].class_name
                }
                
            }
        }
        this.setData({
            tapactive : index,
            drugList:[],
            drugList2:[],
            drugList3:[],
            drugList4:[],
            class_id: that.class_id,
            class_name: that.class_name
        })
        page=0
        this.geturldo()
    },
    //分类选择
    tapisshow(e){
        this.data.isshow = !this.data.isshow;
        this.setData({
            isshow : this.data.isshow
        })
    },
    tapchoose(e){
        var index = e.currentTarget.dataset.index;
        this.data.isshow = !this.data.isshow;
        if(index == -1){
           that.class_id = '',
           that.class_name = '全部'
            
        }else{
            that.class_id = that.drugClassList[index].class_id,
            that.class_name = that.drugClassList[index].class_name
        }
        this.setData({
            isshow : this.data.isshow,
            class_id: that.class_id,
            class_name: that.class_name
        })
        page=0
        this.geturldo()
    },
    inputChange(e){
        this.setData({
            drug_name: e.detail.value
        })
        page=0
        this.geturldo()
    },
    //点击按钮
    tapdrugList(e){
        var this_ = this;
        var type = e.currentTarget.dataset.type;//0目录、分类 1个人用药 2个人、公共模板 3历史处方
        var index = e.currentTarget.dataset.index;//下标
        var num = e.currentTarget.dataset.num;//1保存用药
        if(num == 1){//1保存用药
        getMap = this.data.drugList[index]
            var  icd_id="0"
            var jsonData ={//查看是否重复添加
                "hospital_id" : hospital_id,
                "icd_id" : icd_id,
                "doctor_id" : doctor_id,
                "page" : page,
                "drug_name" : '',
                "drug_type" : obj.drug_type
            }
            var jsonAjax = {
                "url": "doctorSide_personal_prescription_getDoctorDrugList.do",
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getDoctorDrugList2_back);
        }else if(num == 2){//详情
            if(type==0){
                var id = this.data.drugList[index].drug_id
            }else if(type== 1){
                var id = this.data.drugList2[index].drug_id
            }else if(type == 4){
                var id = this.data.prescriptionMap.prescriptionList[index].drug_id
            }
            $service.openPage('../../index_drug_information/index_drug_information?drug_id=' + id)
        }else if(num == 3){//开药
            if(type==0){
                var drugMap = this.data.drugList[index]
            }else if(type== 1){
                var drugMap = this.data.drugList2[index]
            }
            wx.redirectTo({
                url:'../index_add_prescription/index_add_prescription?log_id=' + obj.log_id + '&drugMap=' + $service.jsonToStr(drugMap) + '&isshowType=1&drug_type=' + that.drug_type
            })
        }else if(num == 4){//删除个人用药/个人模板
            if(type==1){
                var drugMap = this.data.drugList2[index]
                var content= '确定要删除当前常用药品？'
            }else if(type== 2){
                var drugMap = this.data.drugList3[index]
                var content= '确定要删除当前常用模板？'
            }
            wx.showModal({
                title: '温馨提示',
                content: content,
                success(res) {
                    if (res.confirm) {
                        if(type==1){//删除个人用药
                            var jsonData={
                            	"doctor_drug_id" :this_.data.drugList2[index].doctor_drug_id
                            }
                            var jsonAjax = {
                                "url": "doctorSide_personal_prescription_deleteDoctorDrug.do",
                                "jsonData": jsonData,
                            }
                            $service.getAjaxData(jsonAjax,function(jsonObj) {
                                $service.showToast('删除成功')
                                page=0
                                this_.geturldo()
                            });
                        }else if(type== 2){//删除个人模板
                            var jsonData={
                            	"prescription_id" :this_.data.drugList3[index].prescription_id
                            }
                            var jsonAjax = {
                                "url": "doctorSide_personal_prescription_deleteUtilPrescription.do",
                                "jsonData": jsonData,
                            }
                            $service.getAjaxData(jsonAjax,function(jsonObj) {
                                 $service.showToast('删除成功')
                                page=0
                                this_.geturldo()
                            });
                        }
                    }
                }
            }) 
        }else if(num == 5  || num == 6 || num == 7){//引用处方5 //查看模板6 7另存模板
            tapMap_type = 0
            if(type== 2){//模板的
                that.diagnosisMap = this_.data.drugList3[index]
                var  prescription_id = this_.data.drugList3[index].prescription_id;
                var url = "publicUtil_getUtilPrescription.do"
            }else if(type == 3){//历史处方的
                var  prescription_id = this_.data.drugList4[index].prescription_id
                var url = "publicUtil_getInquiryLogPrescriptionDetails.do"
            }
             var jsonData={
                "prescription_id" :prescription_id
             }
             var jsonAjax = {
                 "url": url,
                 "jsonData": jsonData,
             }
             $service.getAjaxData(jsonAjax, this.getInquiryLogPrescriptionDetails2_back);
            if(num == 6){
                tapMap_type = 1;
                this.setData({
                    diagnosisMap : that.diagnosisMap,
                    isdetalis:true
                })
            }else if(num == 7){
                tapMap_type = 2;
            }
         }else if(num == 8){//关闭弹窗
             this.setData({
                 isdetalis:false
             })
         }else if(num == 9){
             $service.openPage('../index_add_prescription/index_add_prescription?isshowType=2&drug_type=' + that.drug_type)
         }
    },
    ////1保存用药回调
    doctorSide_personal_prescription_getDoctorDrugList2_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var drugList = data.drugList;//药品列表
        for(var i = 0; i < drugList.length;i++){
            if(drugList[i].drug_id == getMap.drug_id){
                $service.showToast('该药品已存在，请勿重复添加');
                return;
            }
        }
        var jsonData={
            "hospital_id" : hospital_id,//医院id
            "drug_name" : getMap.drug_name,//疾病分类id
            "doctor_id" : doctor_id,//医生主键 0为公告模板
            "drug_id" : getMap.drug_id,//药品id （列表参数）
            "drug_type" : getMap.drug_type//药品类型
        } 
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_addDoctorDrugList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax,function(jsonObj) {
             wx.showToast({"title":'保存成功',"icon":"none",duration: 4000});
        });
    },
    ////引用处方/查看处方
    getInquiryLogPrescriptionDetails2_back: function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data;
        var doctorMap = data.doctorMap;
        var prescriptionMap = data.prescriptionMap;////处方内容
        var prescriptionList = prescriptionMap.prescriptionList;//处方
        var inquiryMap = data.inquiryMap;//诊断信息
        if(tapMap_type == 0){//引用处方
            if(this.data.drug_type == 1){//已选中 中药 、、存缓存
                druglistStorage_z =[];
                druglistStorage_z = prescriptionList
                wx.setStorageSync("druglistStorage_z" + doctor_id,$service.jsonToStr(druglistStorage_z))
            }else{//已选中 西药 、、存缓存
                druglistStorage_x =[];
                druglistStorage_x = prescriptionList
                wx.setStorageSync("druglistStorage_x" + doctor_id,$service.jsonToStr(druglistStorage_x))
            }
            $service.showToast('引用成功')
        }else if(tapMap_type == 1){//查看模板
            for(var i = 0; i < prescriptionMap.prescriptionList.length;i++){
                if(that.drug_type == 1){
                    prescriptionMap.prescription_taking = $service.nothingFun(prescriptionMap.prescription_taking,'--')
                    prescriptionMap.prescription_requirements = $service.nothingFun(prescriptionMap.prescription_requirements,'--')
                    prescriptionMap.prescription_num = $service.nothingFun(prescriptionMap.prescription_num,'--')
                }else{
                    prescriptionMap.prescriptionList[i].details_frequency_str = $service.nothingFun(prescriptionMap.prescriptionList[i].details_frequency_str,'--')
                    prescriptionMap.prescriptionList[i].details_day = $service.nothingFun(prescriptionMap.prescriptionList[i].details_day,'--')
                    prescriptionMap.prescriptionList[i].details_usage = $service.nothingFun(prescriptionMap.prescriptionList[i].details_usage,'--')
                }
            }
            
            this.setData({
                prescriptionMap : prescriptionMap
            })
        }else if(tapMap_type == 2){
            $service.openPage('../../index_template/index_template?detailsList=' + $service.jsonToStr(prescriptionList)+'&drug_type='+that.drug_type +'&prescription_price=' + prescriptionMap.prescription_price)
        }
        
    },
})